local item = class("item",Base)

local itemType = {
	"shell","timer","build","upgrade","boom","reinforce","gearup"
}
item.tag = "item"
item.aabb = true

function item:init(game,x,y,scale,type)
	self.game = game
	self.x = x
	self.y = y
	self.type = type or math.random(1,7)
	self.ax = 8
    self.ay = 8
	self.scale = scale or 1
    self.game.world:add(self,
        self.x-self.ax*self.scale,
        self.y-self.ay*self.scale,
        self.scale*16,self.scale*16)
    self:createData(x,y,scale,type)
    if not SERVER then
    	sound:play("itemdrop")
    end
end

function item.collFilter(me,other)
    return bump.Response_Cross
end

function item:collision(cols)
    for i,col in ipairs(cols) do
		local other = col.other
        if self.destroyed then return end
        if not other.destroyed then
            if other.tag == "tank" then
            	self.collFunc[self.type](self.game,other)
            	self:destroy()
                self.game:onGetItem(tank,item)
                if net.server then
            		net:sendToRoom(self.game.room,"get_item")
            	end
            end
        end
    end
end

function item:checkColl()
	local x,y,cols = self.game.world:check(self,
        self.x - 8*self.scale,
        self.y - 8*self.scale,self.collFilter)
    return self:collision(cols)
end

function item:update()
	if not net.inGame then
		self:checkColl()
	end
end

function item:draw()
	love.graphics.setColor(1, 1, 1, 1)
	texture:drawItem(self.type,self.x,self.y,0,self.scale,self.scale,self.ax,self.ay)
end

local function upgrade(game,tank)
	if not SERVER then
    	sound:play("bonus")
    end
	tank:changeType(math.random(1,8))
end

local function shell(game,tank)
	if not SERVER then
    	sound:play("itemget")
    end
	tank:takeShell()
end

local function timer(game,tank)
	if not SERVER then
    	sound:play("itemget")
    end
	for id, obj in pairs(game.objects) do
		if obj.tag == "tank" and obj.team~=tank.team then
			obj:poweroff(5)
		end
	end
end

local function build(game,tank)
	if not SERVER then
    	sound:play("itemget")
    end
	--game.forces[tank.team].hq:build()
end

local function boom(game,tank)
	if not SERVER then
    	sound:play("itemget")
    end
    if net.server then
        net:sendToRoom(game.room,"boom")
    end
	for id, obj in pairs(game.objects) do
		if obj.tag == "tank" and obj.team~=tank.team then
			obj:damage(tank)
		end
	end
end

local function reinforce(game,tank)
	if not SERVER then
    	sound:play("itemget")
    end
    if game.room and game.room.player then
    	game.room.player.life = game.room.player.life + 1
    end
	--game.forces[tank.team].reinforce = game.forces[tank.team].reinforce + 1
	--net.server:sendToAll("reinforce",tank.team)
end

local function gearup(game,tank)
	if not SERVER then
    	sound:play("levelup")
    end	
	tank:changeType(4)	
end

--[[
local itemType = {
	"shell","timer","build","upgrade","boom","reinforce","gearup"
}]]

item.collFunc = {shell,timer,build,upgrade,boom,reinforce,gearup}

return item